package com.blue.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.TimerTask;
/*
*
 * Created by Administrator on 2019/7/1.
*/

public class BatUtils {
    private static final String ip = "127.0.0.1";
    private static final int nport = 8009;
    private static Boolean flag = false;

    public static void main(String[] args) {
       /* Boolean heartbeat1 = heartbeat();
        System.out.println( heartbeat1 );

        excuteBatFile( "E:\\新建文件夹\\apache-tomcat-8.5.14\\bin\\shutdown.bat", true );*/
        /*while(true) {
            Boolean heartbeat = heartbeat();
            System.out.println( heartbeat );
            if (heartbeat){
                break;
            }
        }*/
        /*try {
            Thread.sleep( 2000 );
            Boolean heartbeat = heartbeat();
            System.out.println( heartbeat );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        /*Boolean heartbeat = heartbeat();
        System.out.println( heartbeat );*/

    }

    public static Boolean heartbeat() {
        /*Timer timer = new Timer();
        timer.schedule( new tomTask(),1000,1000 );
        while(true){
            if (flag){
                timer.cancel();
                return flag;
            }
            break;
        }
        timer.cancel();
        return flag;*/
        try {
            Socket socket = new Socket( ip, nport );
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
            if (!flag) {
                Boolean heartbeat = heartbeat();
                //return flag;
            }
        }
        return flag;
    }
    public static Boolean heartbeat2(String port) {
        /*Timer timer = new Timer();
        timer.schedule( new tomTask(),1000,1000 );
        while(true){
            if (flag){
                timer.cancel();
                return flag;
            }
            break;
        }
        timer.cancel();
        return flag;*/
        try {
            Socket socket = new Socket( ip, Integer.parseInt( port ) );
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
            return flag;
        }
        return flag;
    }

    /**
     * 执行bat文件，
     *
     * @param file          bat文件路径
     * @param isCloseWindow 执行完毕后是否关闭cmd窗口
     * @return bat文件输出log
     */

    public static String excuteBatFile(String file, boolean isCloseWindow) {
        String cmdCommand = null;
        if (isCloseWindow) {
            cmdCommand = "cmd /C start /b " + file;
        } else {
            cmdCommand = "cmd.exe /k start " + file;
        }
        StringBuilder stringBuilder = new StringBuilder();
        Process process = null;
        try {
            System.out.println( cmdCommand );
            process = Runtime.getRuntime().exec( cmdCommand );
            if (process != null) {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader( process.getInputStream(), "utf-8" ) );
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append( line + "\n" );
                    System.out.println( "输出结果为：" + stringBuilder );
                }

                return stringBuilder.toString();
            }
            return "failture";

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static class tomTask extends TimerTask {

        @Override
        public void run() {
            try {
                Socket socket = new Socket( ip, nport );
                flag = true;
                System.out.println( "connct to " + socket.getInetAddress() + "on port " + socket.getPort() + "from port "
                        + socket.getLocalPort() + " of " + socket.getLocalAddress() );
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println( "Tomcat启动失败******" );
                flag = false;
            }
        }
    }
}




